package com.waterworld.watch.chat.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.waterworld.watch.R;
import com.waterworld.watch.chat.adapter.SelectWatchAdapter;
import com.waterworld.watch.chat.interfaces.IChatManager;
import com.waterworld.watch.chat.manager.ChatManager;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.UserInfoBean;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.ScreenAdapterUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectWatchActivity extends BaseActivity {

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.rv_select_watch)
    RecyclerView rv_select_watch;

    private IChatManager iChatManager = ChatManager.getInstance();
    private List<UserInfoBean.WatchUser> mList = new ArrayList<>();
    private SelectWatchAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_watch);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        setViewSize(header_parent, LinearLayout.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this)/10);
        header_back.setVisibility(View.VISIBLE);
        header_title.setText(getString(R.string.select_watch));
        header_title.setVisibility(View.VISIBLE);

        if (iChatManager != null) {
            iChatManager.listManageWatch(new BaseObserverListener<BaseResultBean<UserInfoBean.WatchUser[]>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    Log.e("nh",e.toString());
                }

                @Override
                public void onNext(BaseResultBean<UserInfoBean.WatchUser[]> baseResultBean) {
                    if (baseResultBean.getData() != null) {
                        mList.addAll(Arrays.asList(baseResultBean.getData()));
                    }
                    mAdapter = new SelectWatchAdapter(SelectWatchActivity.this,mList);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(SelectWatchActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    rv_select_watch.setLayoutManager(linearLayoutManager);
                    rv_select_watch.setAdapter(mAdapter);
                }
            });
        }
    }

    @OnClick(R.id.header_back)
    void onBack() {
        finish();
    }
}
